package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Note;
import cn.wolfcode.wolf2w.business.api.domain.NoteContent;
import cn.wolfcode.wolf2w.business.mapper.NoteContentMapper;
import cn.wolfcode.wolf2w.business.mapper.NoteMapper;
import cn.wolfcode.wolf2w.business.query.NoteCondition;
import cn.wolfcode.wolf2w.business.query.NoteQuery;
import cn.wolfcode.wolf2w.business.service.INoteService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.destination.RemoteDestinationServer;
import cn.wolfcode.wolf2w.destination.entity.TaDestination;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 旅游日记Service业务层处理
 * 
 * @author hyz
 * @date 2024-08-02
 */
@Service
@Transactional
public class NoteServiceImpl extends ServiceImpl<NoteMapper,Note> implements INoteService {

    @Autowired
    private NoteMapper noteMapper;

    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @Autowired
    private NoteContentMapper noteContentMapper;

    @Autowired
    private RemoteDestinationServer remoteDestinationServer;

    @Override
    public IPage<Note> queryPage(NoteQuery qo) {
        IPage<Note> page = new Page<>(qo.getCurrentPage(),qo.getPageSize());
        QueryWrapper<Note> qw = new QueryWrapper<>();

//        if(qo.getOrderType() == 1){
//            qw.orderByDesc("create_time");
//        }else{
//            qw.orderByDesc("viewnum");
//        }

        qw.orderByDesc(qo.getOrderBy());

        if(qo.getDayType() != -1){
            NoteCondition dayCondition = NoteCondition.DAY_MAP.get(qo.getDayType());
            qw.between("days",dayCondition.getMin(),dayCondition.getMax());
        }

        if(qo.getConsumeType() != -1){
            NoteCondition consumeCondition = NoteCondition.CONSUME_MAP.get(qo.getConsumeType());
            qw.between("avg_consume",consumeCondition.getMin(),consumeCondition.getMax());
        }

        if(qo.getTravelTimeType() != -1){
            NoteCondition timeCondition = NoteCondition.TIME_MAP.get(qo.getTravelTimeType());
            qw.between("month(travel_time)",timeCondition.getMin(),timeCondition.getMax());
        }

//        return noteMapper.selectPage(page,qw);

        IPage<Note> pageResult = noteMapper.selectPage(page, qw);
        for (Note record : pageResult.getRecords()) {
            R<UserInfo> one = remoteUserInfoService.getOne(record.getAuthorId(), SecurityConstants.INNER);
            record.setAuthor(one.getData());
        }

        return pageResult;
    }

    @Override
    public List<Note> queryViewnumTop3ByDestId(Long destId) {
        List<Note> list = lambdaQuery().eq(Note::getDestId, destId)
                .orderByDesc(Note::getViewnum)
                .last("limit 3")
                .list();
        return list;
    }

    @Override
    public Note queryById(Long id) {
        Note note = noteMapper.selectById(id);
        R<UserInfo> r = remoteUserInfoService.getOne(note.getAuthorId(),SecurityConstants.INNER);
        UserInfo userInfo = r.getData();
        note.setAuthor(userInfo);
        NoteContent content = noteContentMapper.selectById(id);
        note.setContent(content);
        return note;
    }

    @Override
    public void insert(Note note) {
        R<TaDestination> r = remoteDestinationServer.getById(note.getDestId());
        note.setDestName(r.getData().getName());
        note.setAuthorId(SecurityContextHolder.getUserId());
        Date date = new Date();
        note.setCreateTime(date);
        note.setUpdateTime(date);
        note.setStatus("0");
        note.setViewnum(0l);
        note.setReplynum(0l);
        note.setFavornum(0l);
        note.setSharenum(0l);
        note.setThumbsupnum(0l);
        save(note);
        NoteContent noteContent = new NoteContent();
        noteContent.setId(note.getId());
        noteContent.setContent(note.getContent().getContent());
        noteContentMapper.insert(noteContent);
    }


}
